package dryven.view.engine.parser.parts;

import java.io.IOException;
import java.io.Writer;

import dryven.codegen.asm.MethodBuilder;
import dryven.view.ViewDataDictionary;
import dryven.view.engine.component.childzone.ChildZoneCallback;
import dryven.view.engine.parser.CompilationException;
import dryven.view.engine.parser.ViewComponentLocator;
import dryven.view.engine.parser.ViewDataDeclarationCollection;
import dryven.view.engine.parser.scope.StackedScope;
import dryven.view.engine.parser.scope.InstructionScope;

public class ChildZonePart extends StackedScope implements ChildZoneCallback, CompiledViewPart {

	private ViewDataDeclarationCollection _declarations;
	
	public ChildZonePart(InstructionScope parentScope) {
		super(parentScope);
	}

	@Override
	public void render(ViewDataDictionary viewData, Writer out) {
		//TODO add implicit declarations here from strategy
		try {
			for (CompiledViewPart part : getParts()) {
				part.evaluate(viewData, out, _declarations);
			}
		} catch(IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void compile(ViewDataDeclarationCollection declarations,
			ViewComponentLocator locator) throws CompilationException {
		_declarations = declarations;
		for (CompiledViewPart part : getParts()) {
			part.compile(declarations, locator);
		}
	}

	/*
	 * Will never get called, this class is evaluated with render following the ChildZoneCallback interface.
	 * @see dryven.view.engine.parser.parts.CompiledViewPart#evaluate(dryven.view.ViewDataDictionary, java.io.Writer, dryven.view.engine.parser.ViewDataDeclarationCollection)
	 */
	@Override
	public void evaluate(ViewDataDictionary viewData, Writer out,
			ViewDataDeclarationCollection declarations) throws IOException {}

	@Override
	public void writeInstructions(MethodBuilder builder) {}

}
